package com.ruibang.glass.equipment.service.impl.device;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.common.constants.RedisKey;
import com.ruibang.glass.equipment.domain.req.device.DeviceAllPageReq;
import com.ruibang.glass.equipment.entity.device.RepairUrgent;
import com.ruibang.glass.equipment.mapper.device.RepairUrgentMapper;
import com.ruibang.glass.equipment.service.RepairUrgentService;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import com.teaming.cloud.framework2.common.model.ResultBody;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 紧急抢修服务类
 * </p>
 *
 * @author mameng
 * @since 2024-03-20
 */
@Slf4j
@Service
public class RepairUrgentServiceImpl extends ServiceImpl<RepairUrgentMapper, RepairUrgent> implements RepairUrgentService {



    @Autowired
    private RedisSingleService redisService;
    /**
     * 紧急抢修列表
     */
    @Override
    public IPage<RepairUrgent> queryPage(DeviceAllPageReq deviceAllPageReq) {
        return baseMapper.getCheckList(new Page(deviceAllPageReq.getPageNo(), deviceAllPageReq.getPageSize()), deviceAllPageReq);
    }
    /**
     * 跟新修改
     */
    @Override
    public ResultBody updateOrAddById(RepairUrgent repairUrgent) {
        if (StringUtils.isNotBlank(repairUrgent.getResponsiblePerson())){
            Map<Object, Object> userMap = redisService.hmget(RedisKey.USER_GROUP_TYPE);
            if (userMap!=null){
                repairUrgent.setResponsiblePerson(userMap.get(repairUrgent.getResponsiblePerson())==null?"":userMap.get(repairUrgent.getResponsiblePerson()).toString());
                repairUrgent.setResponsiblePersonId(repairUrgent.getResponsiblePerson());
            }
        }
        if (StringUtils.isBlank(repairUrgent.getId())) {
            baseMapper.insert(repairUrgent);
            return ResultBody.success();
        }
        baseMapper.updateById(repairUrgent);
        return ResultBody.success();
    }


}
